﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.Routing;
using FluentAssertions;
using Moq;
using T4MvcJs.RoutesHandler;

namespace T4JS.Tests
{
    public class RouteHelper
    {

        public static string GetUrlFor(RouteCollection routes, string controller, string action, object parameters = null)
        {
            var urlHelper = UrlHelperFactory.CreateUrlHelper(routes);

            return urlHelper.Action(action, controller, parameters);
        }

        public static void AssertRoute(RouteCollection routes, string url,
                object expectations)
        {
            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath)
                .Returns(url);

            var routeData = routes.GetRouteData(httpContextMock.Object);
            routeData.Should().NotBeNull();

            foreach (KeyValuePair<String, object> property in GetProperties(expectations))
            {
                property.Value.ToString().Should().BeEquivalentTo(routeData.Values[property.Key].ToString(), "Key: " + property.Key);
            }
        }
        private static Dictionary<string, object> GetProperties(object values)
        {
            var result = new Dictionary<string, object>();
            if (values != null)
            {
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
                {
                    object obj2 = descriptor.GetValue(values);
                    result.Add(descriptor.Name, obj2);
                }
            }
            return result;
        }
    }
}